class Tank {
  constructor(x, y, team = 'player') {
    this.x = x;
    this.y = y;
    this.rotation = 0;
    this.speed = 5;
    this.size = 48;
    this.team = team;
    this.health = 100;
    this.score = 0;
    this.fireRate = 0.5;
    this.lastFireTime = 0;

    // 加载坦克图片
    this.image = new Image();

    // 创建坦克外观
    const tankCanvas = document.createElement('canvas');
    tankCanvas.width = 48;
    tankCanvas.height = 48;
    const tankCtx = tankCanvas.getContext('2d');

    // 根据阵营设置不同颜色
    if (team === 'player') {
      tankCtx.fillStyle = '#3498db';  // 蓝色玩家
    } else if (team === 'boss') {
      tankCtx.fillStyle = '#f1c40f';  // 黄色首领
    } else {
      tankCtx.fillStyle = '#e74c3c';  // 红色敌人
    }

    // 绘制坦克主体（稍微缩小主体）
    tankCtx.fillRect(8, 8, 32, 32);

    // 绘制炮管（更明显的炮管）
    tankCtx.fillStyle = '#2c3e50';
    // 水平炮管
    tankCtx.save();
    tankCtx.translate(24, 24);  // 移动到坦克中心
    tankCtx.fillRect(-2, -24, 4, 28);  // 炮管从中心向上延伸
    tankCtx.restore();

    // 绘制坦克履带
    tankCtx.fillStyle = '#34495e';
    tankCtx.fillRect(4, 4, 40, 6);   // 上履带
    tankCtx.fillRect(4, 38, 40, 6);  // 下履带

    this.image.src = tankCanvas.toDataURL();
  }

  update(moveInput, rotateInput, map, mapTileSize) {
    // 计算移动方向
    const moveAngle = Math.atan2(moveInput.y, moveInput.x);

    // 只有在有移动输入时才更新旋转
    if (moveInput.x !== 0 || moveInput.y !== 0) {
      // 设置坦克朝向移动方向
      this.rotation = moveAngle;
    }

    // 计算新位置
    const newX = this.x + moveInput.x * this.speed;
    const newY = this.y + moveInput.y * this.speed;

    // 检查碰撞
    if (!this.checkCollision(newX, newY, map, mapTileSize)) {
      this.x = newX;
      this.y = newY;
    }
  }

  checkCollision(newX, newY, map, tileSize) {
    // 获取坦克四个角的位置
    const corners = [
      { x: newX - this.size / 2, y: newY - this.size / 2 },
      { x: newX + this.size / 2, y: newY - this.size / 2 },
      { x: newX - this.size / 2, y: newY + this.size / 2 },
      { x: newX + this.size / 2, y: newY + this.size / 2 }
    ];

    // 检查每个角是否与墙壁碰撞
    for (const corner of corners) {
      const tileX = Math.floor(corner.x / tileSize);
      const tileY = Math.floor(corner.y / tileSize);

      if (tileX < 0 || tileX >= map[0].length || tileY < 0 || tileY >= map.length) {
        return true; // 碰到地图边界
      }

      if (map[tileY][tileX] === 1) {
        return true; // 碰到墙壁
      }
    }

    return false;
  }

  draw(ctx) {
    ctx.save();
    ctx.translate(this.x, this.y);
    ctx.rotate(this.rotation);
    ctx.drawImage(this.image, -this.size / 2, -this.size / 2, this.size, this.size);
    ctx.restore();
  }

  fire() {
    const now = Date.now();
    if (now - this.lastFireTime >= this.fireRate * 1000) {
      this.lastFireTime = now;

      // 计算炮管前端位置
      const barrelLength = 28;  // 炮管长度
      const bulletX = this.x + Math.cos(this.rotation) * barrelLength;
      const bulletY = this.y + Math.sin(this.rotation) * barrelLength;

      return new Bullet(bulletX, bulletY, this.rotation, this.team);
    }
    return null;
  }

  takeDamage(damage) {
    this.health -= damage;
    return this.health <= 0;
  }
}

// 修改子弹类，使其更容易看到
class Bullet {
  constructor(x, y, rotation, team) {
    this.x = x;
    this.y = y;
    this.rotation = rotation;
    this.team = team;
    this.speed = 12;
    this.size = 6;  // 稍微减小子弹大小
    this.damage = team === 'boss' ? 35 : 25;
  }

  update() {
    this.x += Math.cos(this.rotation) * this.speed;
    this.y += Math.sin(this.rotation) * this.speed;
  }

  draw(ctx) {
    ctx.save();
    ctx.translate(this.x, this.y);

    // 根据队伍设置不同颜色
    if (this.team === 'player') {
      ctx.fillStyle = '#3498db';
    } else if (this.team === 'boss') {
      ctx.fillStyle = '#f1c40f';
    } else {
      ctx.fillStyle = '#e74c3c';
    }

    // 绘制子弹主体
    ctx.beginPath();
    ctx.arc(0, 0, this.size / 2, 0, Math.PI * 2);
    ctx.fill();

    // 添加发光效果
    ctx.globalAlpha = 0.3;
    ctx.beginPath();
    ctx.arc(0, 0, this.size, 0, Math.PI * 2);
    ctx.fill();

    // 添加运动轨迹
    ctx.globalAlpha = 0.2;
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(-Math.cos(this.rotation) * 10, -Math.sin(this.rotation) * 10);
    ctx.strokeStyle = ctx.fillStyle;
    ctx.lineWidth = 2;
    ctx.stroke();

    ctx.restore();
  }
} 